#!/bin/bash

###SHELLPACK preamble bonnie-bench 1.03e

DATASET_SIZE=0
NR_FILES=0
MACHINE=`hostname`
FSYNC=no
FSYNC_PARAM=

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam --dataset		DATASET_SIZE
###SHELLPACK parseargParam --nr_files		NR_FILES
###SHELLPACK parseargParam --dirsize		DIRECTORY_SIZE
###SHELLPACK parseargParam --nr_directories	NR_DIRECTORIES
###SHELLPACK parseargYes   --sync		FSYNC
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_install_required bonnie-${VERSION}
###SHELLPACK init_complete

set_mmtests_numactl 0 0

DIRTEST_PARAM="-n 0"
FILETEST_PARAM="-s 0"
if [ $NR_FILES -gt 0 ]; then
	NR_FILES=$((NR_FILES/1024))
	if [ $NR_FILES -eq 0 ]; then
		NR_FILES=1
	fi
	DIRTEST_PARAM="-n $NR_FILES:$DIRECTORY_SIZE:$DIRECTORY_SIZE:$NR_DIRECTORIES"
fi

if [ $DATASET_SIZE -gt 0 ]; then
	DATASET_SIZE=$((DATASET_SIZE/1048576))
	FILETEST_PARAM="-s $DATASET_SIZE"
fi

if [ "$FSYNC" = "yes" ]; then
	FSYNC_PARAM=-b
fi

USER_PARAM=
if [ "`whoami`" = "root" ]; then
	USER_PARAM="-u root"
fi

cd $SHELLPACK_SOURCES/bonnie-${VERSION}-installed

echo ${MMTESTS_NUMACTL} ./sbin/bonnie++ \
	$FSYNC_PARAM -r 0 \
	-d $SHELLPACK_DATA \
	$FILETEST_PARAM \
	$DIRTEST_PARAM \
	-m $MACHINE \
	-R 128 | tee $LOGDIR_RESULTS/bonnie-params

monitor_pre_hook $LOGDIR_RESULTS $P
save_rc ${MMTESTS_NUMACTL} ./sbin/bonnie++ $FSYNC_PARAM $USER_PARAM -r 0 \
	-d $SHELLPACK_DATA \
	$FILETEST_PARAM \
	$DIRTEST_PARAM \
	-m $MACHINE \
	-R 128 \
	-l $LOGDIR_RESULTS/bonnie-detail \
	2>&1 | tee $LOGDIR_RESULTS/bonnie
monitor_post_hook $LOGDIR_RESULTS $P

recover_rc
if [ $? -ne 0 ]; then
	die Failed to run bonnie
fi

gzip $LOGDIR_RESULTS/bonnie-detail

exit $SHELLPACK_SUCCESS
